// GrandiaX Mana Egg Table GeneratorView.cpp : implementation of the CGrandiaXManaEggMagicStatView class
//

#include "stdafx.h"
#include "GrandiaX Mana Egg Table Generator.h"

#include "GrandiaX Mana Egg Table Generator Doc.h"
#include "GrandiaX Mana Egg Magic Stat View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static const int MagicLength = 400;
static const int MagicHeight = 20;
static const int Gap = 5;

static const int MagicWidth = 150;

/////////////////////////////////////////////////////////////////////////////
// CGrandiaXManaEggMagicStatView

IMPLEMENT_DYNCREATE(CGrandiaXManaEggMagicStatView, CScrollView)

BEGIN_MESSAGE_MAP(CGrandiaXManaEggMagicStatView, CScrollView)
	//{{AFX_MSG_MAP(CGrandiaXManaEggMagicStatView)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGrandiaXManaEggMagicStatView construction/destruction

CGrandiaXManaEggMagicStatView::CGrandiaXManaEggMagicStatView()
{
	// TODO: add construction code here

}

CGrandiaXManaEggMagicStatView::~CGrandiaXManaEggMagicStatView()
{
}

BOOL CGrandiaXManaEggMagicStatView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGrandiaXManaEggMagicStatView drawing

void CGrandiaXManaEggMagicStatView::OnDraw(CDC* pDC)
{
}

/////////////////////////////////////////////////////////////////////////////
// CGrandiaXManaEggMagicStatView printing

BOOL CGrandiaXManaEggMagicStatView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CGrandiaXManaEggMagicStatView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CGrandiaXManaEggMagicStatView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CGrandiaXManaEggMagicStatView diagnostics

#ifdef _DEBUG
void CGrandiaXManaEggMagicStatView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CGrandiaXManaEggMagicStatView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CGrandiaXManaEggTableGeneratorDoc* CGrandiaXManaEggMagicStatView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGrandiaXManaEggTableGeneratorDoc)));
	return (CGrandiaXManaEggTableGeneratorDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGrandiaXManaEggMagicStatView message handlers

void CGrandiaXManaEggMagicStatView::OnInitialUpdate() 
{
	CScrollView::OnInitialUpdate();

	int MagicStat[81];
	int MaxUsed = 0;
	memset(MagicStat, 0, sizeof(MagicStat));

	for (int egg=0;egg<42;egg++)
	{
		for (int index=0;index<6;index++)
		{
			int magic = GetDocument()->GetMagic(egg, index);

			if (magic>0)
			{
				magic--;
				MagicStat[magic]++;
				
				if (MagicStat[magic] > MaxUsed)
					MaxUsed = MagicStat[magic];
			}
		}
	}
	
	for (int i=0;i<81;i++)
	{
		if (!MagicNames[i].m_hWnd)
		MagicNames[i].Create(GetDocument()->GetMagicNames(i+1), SS_SIMPLE |WS_CHILD|WS_VISIBLE,
			CRect(Gap, i*(MagicHeight + Gap) + Gap,
			MagicWidth, (i+1)*(MagicHeight+Gap) ), this, 1000+i );

		if (!MagicLens[i].m_hWnd)
		{
			MagicLens[i].Create(PBS_SMOOTH |WS_CHILD|WS_VISIBLE,
				CRect(Gap * 2 + MagicWidth, i*(MagicHeight + Gap) + Gap,
				Gap * 2 + MagicWidth + MagicLength, (i+1)*(MagicHeight+Gap) ), this, 2000+i );
		}

		MagicLens[i].SetRange(0, MaxUsed);
		MagicLens[i].SetPos(MagicStat[i]);
	}
	
	SetScrollSizes(MM_TEXT, CSize(Gap * 3 + MagicWidth + MagicLength, 81*(MagicHeight+Gap) + 2 * Gap) );
}
